import java.awt.*;
import javax.swing.*;
import javax.accessibility.*;

/**
 *
 * @author mith
 */

public class PlotDraw extends JPanel {
  private int points;
  private int line;
  private int border;
  private double[][] y;
  JLayeredPane layeredPane;
  JLabel labelTitle;
  JLabel []labelAxeX;
  JLabel []labelAxeY;

  public PlotDraw(double [][]result, int _line, int _points) {
      y = result;
      line = _line;
      points = _points;
      border = 20; // border of plot - magic number

      // FOR DEBUG
      System.out.println("DEBUG: Plot Draw, line = " + line + ", points = " + points);
      for(int i=0; i<points; i++) {
          System.out.println("d = " + y[i][line]);
      }
      if(result == null) { System.out.println("oj"); return; }
      // END DEBUG

      layeredPane = new JLayeredPane();
      labelTitle = new JLabel("Text-Only Label");
      add(labelTitle);

      Point origin = new Point(border, border);
      labelAxeX = new JLabel[5];
      for(int i=0; i<5; i++) {
          labelAxeX[i] = new JLabel("OX" + i);
//          labelAxeX[i] = createColoredLabel("OX", Color.cyan, origin);
          labelAxeX[i].setVerticalAlignment(SwingConstants.CENTER);
      }
     
    for(int i=0; i<5; i++) {
        add(labelAxeX[i]);
    }

      repaint();
  }

  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int maxWidth = getWidth();
    double hstep = (double)(maxWidth-2*border) / ((double)points-1);
    int maxHeight = getHeight();
    double miny = min(y, line);
    double maxy = max(y, line);

    int []x = new int[points];
    for(int i = 0; i < points; i++) {
      x[i] = (int)((y[i][line] - miny)*(maxHeight-2*border)/(maxy-miny));
    }

    drawGrid(g, border, 5, 4);

    g.setColor(Color.RED);
    for(int i = 1; i < points; i++) {
      int x1 = (int)((i - 1) * hstep)+border;
      int x2 = (int)(i * hstep)+border;
      int y1 = x[i-1]+border;
      int y2 = x[i]+border;
      g.drawLine(x1, y1, x2, y2);
    }

    layeredPane.setPreferredSize(new Dimension(maxWidth, maxHeight));
    add(labelTitle);
    for(int i=0; i<5; i++) {
        add(labelAxeX[i]);
    }
    // drawLabels(g, border, 3, 2);

  }

  double max(double [][]y, int line) {
      double max = y[0][line];
      for(int i=0; i<points; i++) {
          if(max < y[i][line]) {
              max = y[i][line];
          }
      }
      return max;
  }

  double min(double [][]y, int line) {
      double min = y[0][line];
      for(int i=0; i<points; i++) {
          if(min > y[i][line]) {
              min = y[i][line];
          }
      }
      return min;
  }

  void drawGrid(Graphics g, int border, int nx, int ny) {
      int maxWidth = getWidth();
      int maxHeight = getHeight();
      super.paintComponent(g);
      g.setColor(Color.BLACK);
      for(int i=0; i<=nx; i++) {
          g.drawLine(((maxWidth-2*border)*i)/nx+border, border, ((maxWidth-2*border)*i)/nx+border, maxHeight-border);
          g.drawLine(border, ((maxHeight-2*border)*i)/ny+border, maxWidth-border, ((maxHeight-2*border)*i)/ny+border);
      }
  }

  void drawLabels(Graphics g, int border, int nx, int ny) {
      System.out.println("Start drawLabels");
      int maxWidth = getWidth();
      int maxHeight = getHeight();

      System.out.println("Stop drawLabels");
  }

  void drawLine(Graphics g, int x1, int y1, int x2, int y2) {
      super.paintComponent(g);
      g.drawLine(x1, y1, x2, y2);
  }
}
